#!/usr/bin/python
import cmd
import struct
def genFabo(max = 10000):
    """
    This module  is a generator of Fabonacci sequence

    >>> list(genFabo(200))
    [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
    """
    x1 = 1; x2 = 1;
    while x1<max:
        yield x1
        x1,x2 = x2, x1+x2



def pCurdata(file, lines = 2):
    oldPos = file.tell()
    frompos = oldPos - oldPos % 16 - 16
    if frompos<0: frompos = 0
    endpos = frompos + 32
    file.seek(frompos)
    buf = file.read(16 * lines)
    for i in range(len(buf)):
        if i%16 ==0: print "%08X :" % (frompos+i),
        if i+frompos == oldPos:
            sys.stdout.write(",")
        else:
            sys.stdout.write(" ")
        sys.stdout.write("%02x" % ord(buf[i]))
        if (i+1) % 16 == 0: print '\n',
    file.seek(oldPos)
    
def guess(file):
    pass

f21 = open('600021.day','rb')

format = ""

class A(object):
    def met(self):
        print 'A.met'
class B(A):
    def met(self):
        print 'B.met'
        super(B,self).met( )
class C(A):
    def met(self):
        print 'C.met'
        super(C,self).met( )
class D(B,C):
    def met(self):
        print 'D.met'
        super(D,self).met( )

class X(cmd.Cmd):
    src = ""
    disline = 5
    def precmd(self, line):
        if "+" in line[0:1]:
            return "add " + line[1:]
        return line
    def do_s(self, pattern):
        if pattern=="": pattern=X.src;
        print pattern
        str_size = struct.calcsize(pattern)
        oldpos = f21.tell()
        ss = f21.read(str_size)
        f21.seek(oldpos)
        print struct.unpack(pattern, ss)
        X.src = pattern
        pCurdata(f21, str_size/16 + 1)
    def do_add(self, num = 0):
        try:
            if num != 0:
                num=num.strip()
                if num.startswith("0x") or num.startswith("0X"):
                    num = int(num, 16)
                elif num.startswith("0"):
                    num = int(num, 8)
                else :
                    num = int(num)
            if num<0: num = 0
        except:
            num = 0
            print "argument error. auto seek to pos: 0"
        f21.seek(num)
    def do_pr(self, rest):
        if rest =="":
            rest = X.disline
        else:
            try:
                rest = int(rest)
            except:
                rest = disline
        pCurdata(f21, rest)
        X.disline = rest
    def help_pr(self):
        print "print (any string): outputs (any string)"
    def do_stop(self, rest):
        raise StopMe
    def help_stop(self):
        print "stop: terminates the command loop"
    def kki(self):
        pass

D().met()

if __name__=='__main__':
    import doctest, sys
    doctest.testmod(sys.modules[__name__])
    while True:
        X.kki(X())
        X( ).cmdloop()
        
        
